﻿#region Usings

using System;
using System.Globalization;
using System.Linq;

using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Users;
using DotNetNuke.Web.UI.WebControls;

using DataCache = DotNetNuke.UI.Utilities.DataCache;
using System.Web.UI.WebControls;
using System.Collections;
using DotNetNuke.Security.Roles;
using DotNetNuke.Services.Localization;

using BuRU = VLUNewsBusiness.VLURolesAndUsers;

#endregion

namespace VLUNewsUI.VLURolesAndUsers
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The UserSettings PortalModuleBase is used to manage User Settings for the portal
    /// </summary>
    /// <remarks>
    /// </remarks>
    /// -----------------------------------------------------------------------------
    public partial class UserSettings : ModuleSettingsBase
    {
        #region Private Members

        private int _roleGroupId = -1;

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            manageServiceItem.Visible = usersControl.Visible = !IsHostMenu;
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// BindGroups gets the role Groups from the Database and binds them to the DropDown
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <history>
        ///     [cnurse]    01/05/2006  Created
        /// </history>
        /// -----------------------------------------------------------------------------
        private void BindGroups()
        {
            _roleGroupId = BuRU.RoleAndUser.GetNewsRoleGroupID();
            ListItem liItem;
            ArrayList arrGroups = RoleController.GetRoleGroups(PortalId);

            cboRoleGroups.Items.Clear();
            cboRoleGroups.Items.Add(new ListItem(Localization.GetString("None"), "-1"));

            if (arrGroups.Count > 0)
            {
                foreach (RoleGroupInfo roleGroup in arrGroups)
                {
                    liItem = new ListItem(roleGroup.RoleGroupName, roleGroup.RoleGroupID.ToString(CultureInfo.InvariantCulture));
                    if (_roleGroupId == roleGroup.RoleGroupID)
                    {
                        liItem.Selected = true;
                    }
                    cboRoleGroups.Items.Add(liItem);
                }
                divGroups.Visible = true;
            }
            else
            {
                _roleGroupId = -1;
                divGroups.Visible = false;
            }
        }

        public override void LoadSettings()
        {
            BindGroups();

            displayMode.EnumType = "DotNetNuke.Entities.Modules.DisplayMode, DotNetNuke";
            usersControl.EnumType = "DotNetNuke.Entities.Modules.UsersControl, DotNetNuke";

            settingsEditor.DataSource = UserController.GetUserSettings(PortalId);
            settingsEditor.DataBind();

        }

        public override void UpdateSettings()
        {
            _roleGroupId = int.Parse(cboRoleGroups.SelectedValue);
            BuRU.RoleAndUser.UpdateNewsRoleGroupID(_roleGroupId, this.UserId);

            foreach (DnnFormItemBase item in settingsEditor.Items)
            {
                PortalController.UpdatePortalSetting(PortalId, item.DataField,
                                                        item.Value.GetType().IsEnum
                                                            ? Convert.ToInt32(item.Value).ToString(CultureInfo.InvariantCulture)
                                                            : item.Value.ToString()
                                                        );
            }

            //Clear the UserSettings Cache
            DataCache.RemoveCache(UserController.SettingsKey(PortalId));
        }
    }
}